import mi from "../MI";


export default class AudioMgr {
    // 音乐音量大小
    private static bgmVolume: number = 1;
    // 音效音量大小
    private static sfxVolume: number = 1;
    private static bgmAudioID: number = -1;


    public static init(): void {
        var t = cc.sys.localStorage.getItem("bgmVolume");
        if (t != null) this.bgmVolume = parseFloat(t);

        var t = cc.sys.localStorage.getItem("sfxVolume");
        if (t != null) this.sfxVolume = parseFloat(t);

        cc.game.on(cc.game.EVENT_HIDE, this.pauseAll);
        cc.game.on(cc.game.EVENT_SHOW, this.resumeAll);
    }

    public static playBGM(url: string): void {
        this.stopBGM();
        const audioUrl = this.getUrl(url);
        cc.loader.loadRes(audioUrl, (err, clip) => {
            if (!err) {
                this.bgmAudioID = cc.audioEngine.play(clip, true, this.bgmVolume);
            } else {
                cc.error("AudioMgr.playBGM loadRes:" + url + ", err:" + err);
            }
        });
    }

    public static playBGM2(bgmClip: cc.AudioClip): void {
        this.stopBGM();
        this.bgmAudioID = cc.audioEngine.play(bgmClip, true, this.bgmVolume);
    }

    public static stopBGM(): void {
        if (this.bgmAudioID >= 0) cc.audioEngine.stop(this.bgmAudioID);
    }

    public static playSFX(url: string, cb?: Function): void {
        const audioUrl = this.getUrl(url);
        cc.loader.loadRes(audioUrl, (err, clip) => {
            if (!err) {
                const audioId = cc.audioEngine.play(clip, false, this.sfxVolume);
                if (cb) cc.audioEngine.setFinishCallback(audioId, cb);
            } else {
                cc.error("AudioMgr.playSFX loadRes:" + url + ", err:" + err);
            }
        });
    }

    public static playSFX2(sfxClip: cc.AudioClip, cb?: Function): number {
        const audioId = cc.audioEngine.play(sfxClip, false, this.sfxVolume);
        if (cb) cc.audioEngine.setFinishCallback(audioId, cb);
        return cc.audioEngine.getDuration(audioId);
    }

    public static getBGMVolume(): number {
        return this.bgmVolume;
    }

    public static setBGMVolume(v: number, force: boolean = false): void {
        if (this.bgmVolume != v || force) {
            cc.sys.localStorage.setItem("bgmVolume", v);
            this.bgmVolume = v;
            cc.audioEngine.setVolume(this.bgmAudioID, v);
        }
    }

    public static getSFXVolume(): number {
        return this.sfxVolume;
    }

    public static setSFXVolume(v: number): void {
        if (this.sfxVolume != v) {
            cc.sys.localStorage.setItem("sfxVolume", v);
            this.sfxVolume = v;
        }
    }

    public static pauseAll(): void {
        cc.log("cc.audioEngine.pauseAll");
        cc.audioEngine.pauseAll();
    }

    public static resumeAll(): void {
        cc.log("cc.audioEngine.resumeAll");
        cc.audioEngine.resumeAll();
    }


    private static getUrl(url: string): string {
        if (mi.soundPath == '') return url;
        return mi.soundPath + '/' + url;
    }
}
